import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/pages/Login.vue'
import Register from '../components/pages/Register.vue'
import Home from '../components/Home.vue'
import axios from 'axios'
import Welcome from '../components/pages/Welcome.vue'
import Disease from '../components/pages/OneDisease.vue'
import DiseaseGraph from '../components/pages/DiseaseGraph.vue'
import HelpCure from '../components/pages/HelpCure.vue'
import CloudDisease from '../components/pages/CloudDisease.vue'
import CloudUpload from '../components/pages/CloudUpload.vue'
import Admin from '../components/myadmin/Adminmain.vue'
import AdminDisease from '../components/myadmin/Admindisease.vue'
Vue.use(VueRouter)

const routes = [ //添加路由对象
	{
		path: "/",
		redirect: "/login",
		meta: {
			title: '登录页面'
		}
	},
	{
		path: "/register",
		component: Register,
		name: "register",
		meta: {
			title: '注册页面'
		}
	},
	{
		path: "/login",
		component: Login,
		name: "login",
		meta: {
			title: '登录页面'
		}
	},
	{
		path: "/home",
		component: Home,
		redirect: "/welcome",
		children: [{
				path: "/welcome",
				component: Welcome
			},
			{
				path: "/onedisease",
				component: Disease
			},
			{
				path: "/diseaseGraph",
				component: DiseaseGraph
			},
			{
				path: "/helpcure",
				component: HelpCure
			},
			{
				path: "/clouddisease",
				component: CloudDisease
			},
			{
				path: "/cloudupload",
				component: CloudUpload
			}
		]
	},
	{
		path: "/admin",
		component: Admin,
		children: [{
				path: "/admindisease",
				component: AdminDisease
			}
		]
	}
]

const router = new VueRouter({
	mode: 'history', //采用哪种路由模式,默认是history,可改为hash
	routes
})

router.beforeEach((to, from, next) => {
	//to将要访问的路径
	//from从哪里开始访问的
	//next 接近于next(url)，重定向到url上

	const userFlag = window.sessionStorage.getItem("user"); //获取刚存储的用户对象
	if (to.name != "login" && !userFlag) {
		if (to.name == "register") {
			next();
		} else {
			next({
				name: 'login'
			});
		}
	} else {
		next();
	}
	// 
})

export default router